﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using TicTacToe.Core;
using TicTacToe.Core.AI;

namespace TicTacToe.UI
{
    partial class NewGameDialog : Window
    {
        bool mStarting;
        bool mPlayerOneValid;
        bool mPlayerTwoValid;

        bool AllValid { get { return mPlayerOneValid && mPlayerTwoValid; } }

        // o=human,human 1=human,ai
        int mMode;

        public NewGameDialog()
        {
            InitializeComponent();

            var rng = new Random();
            firstPlayerCBox.SelectedIndex = rng.Next(0, 2);

            playerOneControl.SelectedColor = Colors.Red;
            playerTwoControl.SelectedColor = Colors.Blue;

            playerOneControl.PlayerPropsChanged += playerOneControl_PlayerPropsChanged;
            playerTwoControl.PlayerPropsChanged += playerTwoControl_PlayerPropsChanged;

            startButton.IsEnabled = false;
        }

        void playerOneControl_PlayerPropsChanged(bool valid)
        {
            mPlayerOneValid = valid;

            if (valid) playerOneCBItem.Content = playerOneControl.PlayerName;

            if (mMode == 1)
                startButton.IsEnabled = valid;
            else
                startButton.IsEnabled = AllValid;
        }

        void playerTwoControl_PlayerPropsChanged(bool valid)
        {
            mPlayerTwoValid = valid;

            if (valid) playerTwoCBItem.Content = playerTwoControl.PlayerName;

            if (mMode == 0)
                startButton.IsEnabled = AllValid;
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            mStarting = true;

            var p1 = playerOneControl.Player;
            Player p2 = null;
            if (mMode != 0)
            {
                if (mMode == 1)
                {
                    AIDifficultyLevel level;
                    switch (aiDifficultyCBox.SelectedIndex)
                    {
                        case 0:
                            level = AIDifficultyLevel.Easy;
                            break;
                        case 1:
                            level = AIDifficultyLevel.Normal;
                            break;
                        case 2:
                            level = AIDifficultyLevel.Hard;
                            break;
                        case 3:
                            level = AIDifficultyLevel.Insane;
                            break;
                        default:
                            throw new Exception();
                    }
                    p2 = new AIPlayer(level);
                }
            }
            else
                p2 = playerTwoControl.Player;

            TicTacToeGame game = null;
            if (mMode == 0)
                game = firstPlayerCBox.SelectedIndex == 0 ? 
                       new TicTacToeGame(p1, p2) : new TicTacToeGame(p2, p1);
            else if (mMode == 1)
                game = new TicTacToeGame(p1, p2);

            new GameWindow(game).Show();
            Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!mStarting)
                new StartWindow().Show();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var activeTabIndex = tabControl.SelectedIndex;
            mMode = activeTabIndex;

            if (activeTabIndex == 0)
            {
                startButton.IsEnabled = AllValid;
                firstPlayerCBox.IsEnabled = true;
            }
            else if (activeTabIndex == 1)
            {
                startButton.IsEnabled = mPlayerOneValid;
                firstPlayerCBox.IsEnabled = false;
            }
        }
    }
}
